import {
    cancelVerificate,
    createPayment,
    getListForPage,
    getPaymentById,
    getVerificatedByPaymentId,
    updatePayment,
    verificate
} from '../../services/logisticsOrder/payment';

import {message} from 'antd';
import Constants from '../../utils/Constants';

const R = require('ramda');

export default {
    namespace: 'payment',
    state: {
        page: {},
        dataSource: {},
        list: [],
        paymentList: [],
        poundageList: [],
        revenueList: [],
        paymentPage: {},
        revenuePage: {}
    },
    reducers: {
        /**
         * 刷新state
         *
         * @param state
         */
        refreshState(state, { payload }) {
            return { ...state, ...payload };
        }
    },


    effects: {
        /**
         * 分页查询付款
         *
         * @param receiver		供应商名称（收款人）
         * @param status		状态 0.删除 1.草稿 2.待审核 3.待付款 4.已付款 5.已拒绝
         * @return
         */
        *getListForPage({ payload }, { call, put }) {

            const res = yield call(getListForPage, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: res.data.content.list,
                        page: res.data.content.page
                    }
                });
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 新增付款
         *
         * @param paymentForm
         * @param bindingResult
         * @return
         */
        *createPayment({ payload }, { call }) {
            const res = yield call(createPayment, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("付款申请成功！");
                if (payload.onSuccess) payload.onSuccess(res.data.content);
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 更新付款
         *
         * @param paymentForm
         * @param bindingResult
         * @return
         */
        *updatePayment({ payload }, { call, put }) {
            const res = yield call(updatePayment, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("付款审核成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 根据id获取付款
         *
         * @param id
         * @return
         */
        *getPaymentById({ payload }, { call, put }) {
            if (payload.id) {
                const res = yield call(getPaymentById, { ...R.omit(['onSuccess', 'onFail'])(payload) });
                if (res && res.data.resultCode === "ok") {
                    yield put({
                        type: 'refreshState',
                        payload: {
                            dataSource: res.data.content
                        }
                    });
                } else {
                    if (payload.onFail) payload.onFail();
                }
            } else {
                yield put({
                    type: 'refreshState',
                    payload: {
                        dataSource: {}
                    }
                });
            }
        },
        /**
         * 根据付款id查询已核销的费用
         *
         * @param paymentId
         */
        *getVerificatedByPaymentId({ payload }, { call, put }) {
            const res = yield call(getVerificatedByPaymentId, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                const temp = ['paymentList', 'revenueList', 'poundageList'];
                const temp2 = ['paymentPage', 'revenuePage'];
                yield put({
                    type: 'refreshState',
                    payload: {
                        [temp[payload.type]]: res.data.content.list ? res.data.content.list : [],
                        [temp2[payload.type]]: res.data.content.page ? res.data.content.page : Constants.DEFAULT_PAGE,
                    }
                });
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 付款核销费用     --付款管理  核销费用
         *
         * @param paymentId
         * @param feeId
         * @param amount
         * @return
         */
        *verificate({ payload }, { call, put }) {
            // if(payload.id){
            const res = yield call(verificate, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        dataSource: res.data.content
                    }
                });
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },


        /**
         * 取消核销费用
         *
         * @param id
         * @return
         */
        *cancelVerificate({ payload }, { call, put }) {
            // if(payload.id){
            const res = yield call(cancelVerificate, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        dataSource: res.data.content
                    }
                });
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
            // } else {
            //     yield put({
            //         type: 'refreshState',
            //         payload: {
            //             dataSource: {}
            //         }
            //     });
            // }
        },
    },
    subscriptions: {
        setup({ history, dispatch }) {
            return history.listen(({ pathname }) => {
                const menus = JSON.parse(sessionStorage.getItem(Constants.USER_MENU));
                if(menus && !menus.find(item=>item.includes(pathname)) && pathname != '/logout'){
                    document.location.href = "/notAuthorized"
                }
            });
        }
    }
};